package com.hansheng.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by peyppicp on 16/7/13.
 */
public class PrintWriterHelper {
    static PrintWriter printWriter;
    static HttpServletResponse httpServletResponse;
    public PrintWriterHelper()  {
    }
    private static PrintWriter getPrintWriter() throws IOException {
        httpServletResponse = ServletActionContext.getResponse();
        printWriter=httpServletResponse.getWriter();
        return printWriter;
    }

    public static void send( JSONObject jsonObject) throws IOException {
        printWriter=getPrintWriter();
        printWriter.write(jsonObject.toString());
        printWriter.flush();
        printWriter.close();
    }

    public static void send(JSONArray jsonArray) throws IOException {
        printWriter=getPrintWriter();
        printWriter.write(jsonArray.toString());
        printWriter.flush();
        printWriter.close();
    }

    public static void send(String jsonString) throws IOException {
        printWriter=getPrintWriter();
        printWriter.write(jsonString);
        printWriter.flush();
        printWriter.close();
    }


}
